package org.exam.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.exam.blog.domain.ArticleType;
import org.exam.blog.dto.ArticleDto;
import org.exam.blog.mapper.ArticleTypeMapper;
import org.exam.blog.query.ArticleTypeQuery;
import org.exam.blog.service.IArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ArticleTypeServiceImpl implements IArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void add(ArticleType articleType) {
        articleType.setUpdateTime(new Date());
        articleTypeMapper.add(articleType);

    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void delete(Long id) {
        articleTypeMapper.delete(id);
    }

    @Override
    public ArticleType get(Long id) {
        return articleTypeMapper.get(id);
    }

    @Override
    public List<ArticleType> getAll() {
        return articleTypeMapper.getAll();
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void update(ArticleType articleType) {
        articleType.setUpdateTime(new Date());
        articleTypeMapper.update(articleType);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")

    public void patchDelete(Long[] ids) {
        articleTypeMapper.patchDelete(ids);
    }

    @Override
    public PageInfo<ArticleType> queryPage(ArticleTypeQuery articleTypeQuery) {
        PageHelper.startPage(articleTypeQuery.getCurrentPage(), articleTypeQuery.getPageSize());
        List<ArticleType> data = articleTypeMapper.queryPage(articleTypeQuery);
        return new PageInfo<>(data);
    }

    private List<ArticleType> getTypeTree() {
        List<ArticleType> typeTree = new ArrayList<>();
        List<ArticleType> types = articleTypeMapper.getAll();
        Map<Long, ArticleType> map = new HashMap<>();
        for (ArticleType type : types) {
            map.put(type.getId(), type);
        }
        for (ArticleType type : types) {
            if (type.getParentId() == null) {
                typeTree.add(type);
            } else {
                Long parentId = type.getParentId();
                ArticleType parentType = map.get(parentId);
                if (parentId != null) {
                    List<ArticleType> children = parentType.getChildren();
                    children.add(type);

                }
            }
        }
        return typeTree;
    }

    @Override
    @Cacheable(cacheNames = "articleType", key = "'typeTree'")
    public List<ArticleType> typeTree() {
        return getTypeTree();
    }

    @Override
    public List<ArticleDto> get1() {
       return articleTypeMapper.get1();
    }

    @Override
    public List<ArticleDto> get2() {
        return articleTypeMapper.get2();
    }
}

